const webpack = require("webpack");
const autoprefixer = require("autoprefixer");
const pxtorem = require("postcss-pxtorem");
const path = require("path");

/**
 * Override convention configuration
 * https://cli.vuejs.org/config/
 */
const CompressionPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = ["html", "js", "css"];
const isProd = process.env.NODE_ENV === "production";

// 读取文件路径
function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  // publicPath: "/",
  publicPath: "./",
  // publicPath: "/",
  outputDir: "zutai-h5",
  productionSourceMap: false, // disable source map in production
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "@/assets/scss/common.scss";'
      },
      postcss: {
        plugins: [
          autoprefixer(),
          pxtorem({
            selectorBlackList: [".not-rem", "viewer"], // 要忽略的选择器并保留为px。
            propList: ["*"],
            minPixelValue: 2, // 设置要替换的最小像素值。
            // rootValue: 75, // 换算的基数(设计图750的根字体为32)
            rootValue({ file }) {
              // 判断是否是vant的文件 如果是就使用 37.5为根节点字体大小
              // 否则使用75 因为vant使用的设计标准为375 但是市场现在的主流设置尺寸是750
              return file.indexOf("van") !== -1 ? 37.5 : 75;
            }
          })
        ]
      }
    }
  },

  configureWebpack: config => {
    if (process.env.NODE_ENV === "production") {
      // 打包后去掉console.log
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;

      // 生产环境
      config.plugins.push(
        new CompressionPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
          compressionOptions: { level: 1 },
          threshold: 10240, // 只有大小大于该值的资源会被处理。单位是 bytes。默认值是 0。
          minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理。默认值是 0.8。
          deleteOriginalAssets: false // 删除原文件
        })
      );

      // 模板打包
      // config.optimization = {
      //   splitChunks: {
      //     automaticNameDelimiter: "-",
      //     minSize: 30000,
      //     maxSize: 700000,
      //     chunks: "async"
      //   }
      // }

      // 优化
      config.optimization = {
        splitChunks: {
          chunks: "all",
          cacheGroups: {
            vue: {
              name: "chunk-vue",
              priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
              test: /[\\/]node_modules[\\/]_?vue(.*)/ // in order to adapt to cnpm
            },
            vuex: {
              name: "chunk-vuex",
              priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
              test: /[\\/]node_modules[\\/]_?vuex(.*)/ // in order to adapt to cnpm
            },
            vant: {
              name: "chunk-vant",
              priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
              test: /[\\/]node_modules[\\/]_?vant(.*)/ // in order to adapt to cnpm
            },
            echarts: {
              name: "chunk-echarts",
              priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
              test: /[\\/]node_modules[\\/]_?echarts(.*)/ // in order to adapt to cnpm
            },
            // components: {
            //   name: "chunk-components",
            //   test: resolve("src/components"), // can customize your rules
            //   minChunks: 1, //  minimum common number
            //   priority: 20,
            //   reuseExistingChunk: true
            // },
            libs: {
              name: "chunk-libs",
              test: /[\\/]node_modules[\\/]/,
              priority: 5,
              chunks: "initial" // only package third parties that are initially dependent
            }
          }
        },
        runtimeChunk: "single"
      };
    }
  },
  devServer: {
    disableHostCheck: true, // 花生壳显示Invalid Host header让其不检查hostname。
    https: false, // https:{type:Boolean}
    open: false, // 配置自动启动浏览器
    proxy: {
      "/xvUHiJZSmB.php": {
        target: "http://ydzh.yunlink.hn.cn",
        changeOrigin: true
      }
    }
    // before: require("./mock")
  }
};
